<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" media-type="text/plain"/>
	<xsl:param name="MySubjectArea"/>
	<xsl:template match="/">
		<busMatrix>
			<xsl:for-each select="//SubjectArea">
				<SubjectArea>
					<xsl:copy-of select="SubjectAreaName"/>
					<xsl:copy-of select="SubjectAreaID"/>
					<xsl:copy-of select="BusinessCatalogName"/>
					<xsl:copy-of select="BusinessCatalogID"/>
					<LogicalTableIDList>
						<xsl:for-each select="PresentationColumnList/ReferencedPresentationColumnID">
							<xsl:variable name="PresColID" select="."/>
							<!-- finds the presentation column for that referenced one in the presentation table -->
							<xsl:for-each select="//PresentationColumn[@id = $PresColID]">
								<xsl:variable name="LogColID" select="RefLogicalColumn/@id"/>
								<!-- finds the logical table for the referenced logical column -->
								<xsl:for-each select="//LogicalTable/Columns/RefLogicalColumn[@id = $LogColID]">
									<xsl:variable name="bmID" select="../../@parentId"/>
									<LogicalTableID>
									<xsl:variable name="BizModelName" select="//BusinessModelList/BusinessModel[@id = $bmID]/@name"/>
									<xsl:variable name="LogTableName" select="../../@name"/>
										<xsl:attribute name="joins">
											<xsl:value-of select="count(//LogicalJoin/LogicalTableID[text() = concat($BizModelName,'.',$LogTableName) and @type='DIM'])"/>
										</xsl:attribute>
										<xsl:value-of select="$BizModelName"/>.<xsl:value-of select="$LogTableName"/>
									</LogicalTableID>
								</xsl:for-each>
							</xsl:for-each>
						</xsl:for-each>
					</LogicalTableIDList>
				</SubjectArea>
			</xsl:for-each>
			<!-- copying all business models -->
			<xsl:copy-of select="//BusinessCatalog"/>
			<!-- copying all logical joins (simplified view) -->
			<xsl:copy-of select="//LogicalJoin"/>
		</busMatrix>
	</xsl:template>
</xsl:stylesheet>
